package com.samsung.service.impl.timer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.samsung.dao.timer.TimerEventMapper;
import com.samsung.dao.timer.TimerRecordMapper;
import com.samsung.dto.timer.TimerEventDto;
import com.samsung.model.timer.TimerEvent;
import com.samsung.service.timer.TimerService;

@Service("timerIndexService")
public class TimerServiceImpl implements TimerService {

	@Resource
	private TimerEventMapper timerEventMapper;

	@Resource
	private TimerRecordMapper timerRecordMapper;

	@Override
	public List<TimerEventDto> getStopWatchInfo(int startPos, int pageSize) {
		List<TimerEventDto> timerEventDtoInfo = new ArrayList<TimerEventDto>();
		List<TimerEvent> timerEventInfo = new ArrayList<TimerEvent>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("startPos", startPos);
		paramMap.put("pageSize", pageSize);
		timerEventInfo = timerEventMapper.selectTimerEventsPageInfo(paramMap);
		for (TimerEvent timerEvent :timerEventInfo) {
			TimerEventDto dto = new TimerEventDto();
			int countNumber = 0;
			countNumber = timerRecordMapper.getCountNumber(timerEvent.getId());
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dto.setCreateTime(df.format(timerEvent.getCreateTime()));
			dto.setTimerEvent(timerEvent);
			dto.setCountNumber(countNumber);
			timerEventDtoInfo.add(dto);
		}
		return timerEventDtoInfo;
	}

	
	@Override
	public List<TimerEventDto> getMyTimerInfo(String uid, String title, String createTime, int startPos, int pageSize) {
		List<TimerEventDto> timerEventDtoInfo = new ArrayList<TimerEventDto>();
		List<TimerEvent> timerEventInfo = new ArrayList<TimerEvent>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("startPos", startPos);
		paramMap.put("pageSize", pageSize);
		paramMap.put("createTime", createTime);
		paramMap.put("title", title);
		paramMap.put("uid", uid);
		timerEventInfo = timerEventMapper.selectMyTimerEventsPageInfo(paramMap);
		for (TimerEvent timerEvent :timerEventInfo) {
			TimerEventDto dto = new TimerEventDto();
			int countNumber = 0;
			countNumber = timerRecordMapper.getCountNumber(timerEvent.getId());
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dto.setCreateTime(df.format(timerEvent.getCreateTime()));
			dto.setTimerEvent(timerEvent);
			dto.setCountNumber(countNumber);
			timerEventDtoInfo.add(dto);
		}
		return timerEventDtoInfo;
	}
	
	@Override
	public int deleteTimer(String tid) {
		//删除及时记录
		timerRecordMapper.deleteByEvent(tid);
		//删除主表
		return timerEventMapper.deleteByPrimaryKey(tid);
	}
	
	@Override
	public int getTimerInfoCount() {
		return	timerEventMapper.selectTimerEventsInfo();
	}
	
	@Override
	public int getMyTimerEventsCount(String uid, String title, String createTime) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("createTime", createTime);
		paramMap.put("title", title);
		paramMap.put("uid", uid);
		
		return	timerEventMapper.selectMyTimerEventsCount(paramMap);
	}
	
}

